home *** CD-ROM | disk | FTP | other *** search
/ Plug-In Power Pack for Netscape Communicator / Plug-In Power Pack for Netscape Communicator.iso / plugins / dataviews / dvtools / examples / programs / buf_rebind.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-08  |  11.3 KB  |  381 lines

  1. #ifndef lint
  2. static char SccsId[]= "@(#)buf_rebind.c    V1.18    3/22/95";
  3. #endif
  4.  
  5. /*
  6. |    file name - buf_rebind.c
  7. |===================================================================
  8. |
  9. |     This program is an example of how you might get data into your
  10. |     views without using the data source facility.  The method
  11. |     is called Rebinding.  This method creates a link between the
  12. |     variable descriptors (vdps) associated with the dynamic
  13. |     objects and program variables.
  14. |
  15. |     In this way, the program variables are the source of data
  16. |     for the variable descriptors rather than the data source
  17. |     variables.
  18. |
  19. |     Typing a <q|Q> or the right mouse button will exit the program.
  20. |
  21. |===================================================================
  22. */
  23.  
  24. /* DV-Tools header files */
  25. #include "std.h"             /* <stdio.h> etc., scalar & macro definitions */
  26. #include "dvstd.h"           /* public types & constants */
  27. #include "dvtools.h"         /* constants used by T routines */
  28. #include "dvGR.h"            /* constants used by window mgt & GR routines */
  29. #include "VOstd.h"           /* constants used by VO & VOob routines */
  30. #include "Tfundecl.h"        /* T routines (screens, drawports & views) */
  31. #include "VOfundecl.h"       /* VO routines (objects) */
  32. #include "VGfundecl.h"       /* VG routines (get info from dgp & vdp) */
  33. #include "GRfundecl.h"       /* GR routines (interface to display device)
  34.  
  35. #include <windows.h>
  36.  
  37. /* Constants */
  38. #define  DVPATH            (char *)NULL
  39. #define  DISPFORMS_STB     (char *)NULL
  40. #define  DVDEVICE          (char *)NULL
  41. #define  DVCOLORTABLE      (char *)NULL
  42. #define  VIEW_NAME         "buffer.v"
  43. #define  SCREEN_VIEWPORT   (RECTANGLE *)NULL
  44. #define  DRAWING_VIEWPORT  (RECTANGLE *)NULL
  45.  
  46. /*
  47.  *   Declare the buffers to which the variable descriptors will be
  48.  *   rebound and receive their data.
  49.  */
  50. LOCAL char TextBuf[80];         /* program buffer for vdp named "Text Var" */
  51. LOCAL float FloatBuf;           /* program buffer for vdp named "Float Var" */
  52. LOCAL int counter = 0;          /* init counter used in UpdateData routine */
  53.  
  54. /* Local forward function declarations */
  55. LOCAL ADDRESS RebindVdps V_P_((OBJECT data_obj, ADDRESS vdp, ADDRESS argblock));
  56. LOCAL void UpdateData V_P_((void));
  57.  
  58.  
  59. /*
  60.  *   MAIN PROGRAM
  61.  */
  62. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  63.                      LPSTR lpCmdLine, int nCmdShow )
  64. {
  65.   INT argc = 0;
  66.   CHAR **argv;
  67.   /*
  68.    *  program arguments
  69.    *    argv[1] - display device (default is DVDEVICE)
  70.    */
  71.  
  72.   /* Define & initialize device name and view filename */
  73.    char *device_name = DVDEVICE; /* default device name */
  74.   char *view_name = VIEW_NAME;  /* default view name */
  75.  
  76.   /* Define display variables */
  77.   OBJECT screen;           /* display device, the window */
  78.   DRAWPORT drawport;       /* how & where to display picture, picture frame */
  79.   VIEW view;               /* picture representation of the view file */
  80.  
  81.   /* Control loop variables */
  82.   OBJECT location;              /* the event representation */
  83.  
  84.   /* Other variables */
  85.   OBJECT drawing;               /* graphical representation of screen */
  86.   int Quit = NO;                /* flag to quit program */
  87.  
  88.   /*-----------------
  89.    *   Initialization
  90.    *
  91.    *   TInit:    perform the initialization of DV-Tools
  92.    *             TInit reads your configuration file and any
  93.    *             environment variables or logical names set.
  94.    */
  95.   make_argv(&argc,&argv,GetCommandLine());
  96.   TInit( DVPATH, DISPFORMS_STB );
  97.  
  98.   /*
  99.    *   TscOpenSet:  opens a device as a screen object using
  100.    *                specified attributes
  101.    *
  102.    *   Set exposure block to YES to insure the window
  103.    *   is ready for drawing when TdpDraw is called.
  104.    */
  105.   if (argc > 1)
  106.     device_name = argv[1];
  107.   screen = TscOpenSet (device_name, DVCOLORTABLE,
  108.                        V_X_EXPOSURE_BLOCK, YES,
  109.                        V_ACTIVE_CURSOR, V_END_OF_LIST);
  110.   if (!screen)
  111.     {
  112.       printf ("Must specify device on command line or");
  113.       printf (" in DataViews configuration file.\n");
  114.       S_EXIT (EXIT_ERR);
  115.     }
  116.  
  117.   /*
  118.    *   VOscWinEventMask:  sets the screen's window event mask
  119.    */
  120.   VOscWinEventMask ((ULONG) V_KEYPRESS | V_BUTTONPRESS | V_EXPOSE | V_RESIZE,
  121.             (ULONG) 0);
  122.  
  123.   /*
  124.    *   TviLoad:   Load a view in from a file,
  125.    *              user supplied view or default view of buffer.v
  126.    */
  127.   view = TviLoad (view_name);
  128.   if (!view)
  129.     {
  130.       printf ("Could not load view from file ");
  131.       printf ("%s.\n", view_name);
  132.       S_EXIT (EXIT_ERR);
  133.     }
  134.  
  135.   /*
  136.    *  TviGetDrawing:  Gets a view's drawing object
  137.    *  TobForEachVdp:  Traverses all variable descriptors
  138.    *                  in an object
  139.    *
  140.    *  Traverse all variable descriptors in the drawing object
  141.    *  call the function RebindVdps for each variable descriptor.
  142.    */
  143.   drawing = TviGetDrawing (view);
  144.   TobForEachVdp (drawing, RebindVdps, (ADDRESS) NULL);
  145.  
  146.   /*
  147.    *   TdpCreate: Create a drawport.
  148.    *              The drawport is attached to the screen object
  149.    *              specified while view specifies the view to be
  150.    *              displayed on the screen.
  151.    *
  152.    *   UpdateData:  Update internal buffers used by dynamic
  153.    *                objects in view.
  154.    */
  155.   drawport = TdpCreate (screen, view, SCREEN_VIEWPORT, DRAWING_VIEWPORT);
  156.   UpdateData ();
  157.  
  158.   /*
  159.    *   TscErase: Erase the entire screen in the default
  160.    *             background color
  161.    *   TdpDraw:  Draw the contents of the drawport
  162.    */
  163.   TscErase (screen);
  164.   TdpDraw (drawport);
  165.  
  166.   /*--------------------
  167.    *   Control loop
  168.    *
  169.    *   Poll the event queue for locator events. If the key
  170.    *   represents the character 'q' or 'Q' or the right
  171.    *   mouse button then quit the program. Meanwhile,
  172.    *   continue to update the data buffers and draw the
  173.    *   next iteration of the drawport.
  174.    */
  175.   FOREVER
  176.   {
  177.     /*
  178.      *  VOloWinEventPoll:  Poll for the next window event.
  179.      *                     The polling mode used is V_WAIT.
  180.      *                     Therefore, VOloWinEventPoll does not
  181.      *                     return until a masked event is
  182.      *                     generated.  V_WAIT always produces
  183.      *                     a valid location object.
  184.      */
  185.     location = VOloWinEventPoll (V_WAIT);
  186.  
  187.     /*
  188.      *  VOloType:  returns the type of event.  These types
  189.      *             match event types specified in VOscWinEventMask.
  190.      */
  191.     switch (VOloType (location))
  192.       {
  193.  
  194.       case V_RESIZE:
  195.         /*
  196.          *  The window size has been changed.
  197.          *  TscReset:  Resets all screen drawports after
  198.          *             window resizing
  199.          */
  200.         TscReset (screen);
  201.         break;
  202.  
  203.       case V_EXPOSE:
  204.         /*
  205.          *  VOloRegion:  Returns a rectangle representing the
  206.          *               exposed region on the screen.
  207.          *  TscRedraw:   After erasing, redraws all the drawports
  208.          *               in the screen.
  209.          *  A portion of the window has been exposed and needs
  210.          *  to be redrawn.
  211.          */
  212.         TscRedraw (screen, VOloRegion (location));
  213.         break;
  214.  
  215.       case V_KEYPRESS:
  216.         /*
  217.          *  Check key selected.
  218.          *  VOloKeySym:  Returns the key symbol value of the
  219.          *               location object
  220.          *
  221.          *  If the key symbol represents the characters 'q'
  222.          *  or 'Q' then quit the program.
  223.          */
  224.         switch (VOloKeySym (location))
  225.           {
  226.           case 'q':
  227.           case 'Q':
  228.             Quit = YES;
  229.             break;
  230.  
  231.           default:
  232.             /*
  233.              *  TdpDrawNext: Update all dynamic objects within a
  234.              *               drawport's view.
  235.              *  Update internal data buffers and draw next iteration
  236.              */
  237.             UpdateData ();
  238.             TdpDrawNext (drawport);
  239.             break;
  240.           }
  241.         break;
  242.  
  243.       case V_BUTTONPRESS:
  244.         /*
  245.          *  VOloButton:  Returns the button that was pressed
  246.          *
  247.          *  The right mouse button exits the program.
  248.          */
  249.         if (VOloButton (location) == 3)
  250.           Quit = YES;
  251.         else
  252.           {
  253.             /*
  254.              *  TdpDrawNext: Update all dynamic objects within a
  255.              *               drawport's view.
  256.              *  Update internal data buffers and draw next iteration
  257.              */
  258.             UpdateData ();
  259.             TdpDrawNext (drawport);
  260.           }
  261.         break;
  262.  
  263.       default:
  264.         break;
  265.       }
  266.  
  267.     /* exit the program */
  268.     if (Quit == YES)
  269.       break;
  270.  
  271.   }
  272.  
  273.   /*--------------------
  274.    *   Termination
  275.    *
  276.    *   TscErase:     Erase the entire screen in the default
  277.    *                 background color
  278.    *   TdpDestroy:   Destroy the drawport,
  279.    *   TviDestroy:   Destroy the view, freeing the allocated memory
  280.    *   TscCloseCurrentScreen:  Close the current display screen
  281.    *   TTerminate:   Perform the clean-up for DV-Tools
  282.    */
  283.   TscErase (screen);
  284.   TdpDestroy (drawport);
  285.   TviDestroy (view);
  286.   TscCloseCurrentScreen ();
  287.   TTerminate ();
  288.   return EXIT_OK;
  289. }
  290.  
  291.  
  292. /*
  293.  *-------------
  294.  *   RebindVdps -- modify the variable descriptor to use
  295.  *     our own program variable as the memory buffer.
  296.  *     The variable descriptor which had previously
  297.  *     pointed to a data source variable for the data
  298.  *     will now look at our program variable for the
  299.  *     data information.
  300.  *
  301.  *     The parameter data_obj represents the object that
  302.  *     the variable descriptor is attached to.
  303.  */
  304. /*ARGSUSED*/
  305. LOCAL ADDRESS 
  306. RebindVdps (data_obj, vdp, argblock)
  307.      OBJECT data_obj;
  308.      ADDRESS vdp;
  309.      ADDRESS argblock;
  310. {
  311.   char *name;                   /* variable descriptor name */
  312.  
  313.   /*
  314.    *   VGvdvarname:  Gets a pointer to the variable name
  315.    *
  316.    *   The name of the variable descriptor is used to
  317.    *   determine which buffer to rebind to.  If the
  318.    *   variable does not have a name then return.
  319.    */
  320.   name = VGvdvarname (vdp);
  321.   if (!name)
  322.     return V_CONTINUE_TRAVERSAL;
  323.  
  324.   /*
  325.    *   TvdPutBuffer:  Sets a new variable descriptor buffer
  326.    *
  327.    *   Rebind variable descriptor pointer to internal program
  328.    *   variable.  Program variable will be updated for
  329.    *   dynamic objects.
  330.    */
  331.   if (strcmp (name, "Text Var") == 0)
  332.     TvdPutBuffer (vdp, TextBuf);
  333.   else if (strcmp (name, "Float Var") == 0)
  334.     TvdPutBuffer (vdp, (ADDRESS) &FloatBuf);
  335.  
  336.   return V_CONTINUE_TRAVERSAL;
  337. }
  338.  
  339.  
  340. /*
  341.  *-------------
  342.  *   UpdateData -- updates the values in the buffers of the
  343.  *     dynamic elements of the view.
  344.  */
  345. LOCAL void
  346. UpdateData ()
  347. {
  348.   LOCAL char *mycharbuf[] =
  349.     {
  350.     "1st time",
  351.     "2nd time",
  352.     "3rd time",
  353.     "4th time",
  354.     "5th time",
  355.     "6th time",
  356.     "7th time",
  357.     "8th time",
  358.     "9th time",
  359.     "10th time"
  360.     };
  361.  
  362.   /*
  363.    *  Increment the counter by one, copy the character string
  364.    *  based on the counter value to the program buffer TextBuf.
  365.    */
  366.   if (++counter > 10)
  367.     counter = 1;
  368.  
  369.   strcpy (TextBuf, mycharbuf[counter-1]);
  370.  
  371.   /*
  372.    *  Set the program variable FloatBuf by incrementing it's
  373.    *  current value by .1.  Reset FloatBuf to zero when the
  374.    *  the counter has been reset.
  375.    */
  376.   if (counter == 1)
  377.     FloatBuf = 0.0;
  378.   else
  379.     FloatBuf += 0.1;
  380. }
  381.